package heima.lm.oschina.vpfragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.adapter.LatestTweetRvAdapter;
import heima.lm.oschina.base.BaseFragment;
import heima.lm.oschina.bean.Tweet;
import heima.lm.oschina.bean.TweetsList;
import heima.lm.oschina.constant.Constants;
import heima.lm.oschina.interf.OnRecyclerViewItemClickListener;
import heima.lm.oschina.ui.activity.TweetDetailActivity;
import heima.lm.oschina.utils.HttpUtils;

/**
 * Created by 金毛兄弟 on 2017/4/2.
 * 最新动弹
 */

public class LatestTweetFragment extends BaseFragment {

    @BindView(R.id.srl_latest_sweet)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_latest_tweet)
    RecyclerView mRecyclerView;
    private List<Tweet> mList;


    @Override
    protected void loadSuccessPager() {
        View successView = View.inflate(BaseApplication.mApp, R.layout.fragment_latest_tweet, null);
        ButterKnife.bind(this, successView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseApplication.mApp);
        mRecyclerView.setLayoutManager(layoutManager);
        LatestTweetRvAdapter adapter = new LatestTweetRvAdapter(mList);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent = new Intent();
                intent.setClass(BaseApplication.mApp, TweetDetailActivity.class);
                startActivity(intent);
            }
        });
        mCommonPager.changeView(successView);
    }

    @Override
    protected void loadData() {
        HttpUtils<TweetsList> httpUtils = new HttpUtils<>();
        Map<String, String> params = new HashMap<>();
        params.put("page", "0");
        httpUtils.getData(Constants.Http.TWEET_LIST, params, TweetsList.class, new HttpUtils.OnHttpResponseListener<TweetsList>() {
            @Override
            public void onSuccess(TweetsList bean) {
                mList = bean.getTweetslist();
                mCommonPager.dealCommonSuccess(mList);
            }

            @Override
            public void onFailure(Exception e) {
                mCommonPager.dealCommonFailure();
            }
        });
    }
}
